﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.DirectX.DirectSound;
using System.Diagnostics;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for ConfigControl.xaml
    /// </summary>
    public partial class EditListenersControl : UserControl
    {
        private delegate void VoidInvokeHandler();

        Congregation congregation;
        Sharing sharing;

        Listener selectedListener = null;

        public EditListenersControl(Congregation _congregation, Sharing _sharing)
        {
            congregation = _congregation;
            sharing = _sharing;
            InitializeComponent();
            DrawListeners();


            sharing.ListenerDeleted += new Sharing.ListenerEventHandler(sharing_ListenerDeleted);
            sharing.ListenerAdded += new Sharing.ListenerEventHandler(sharing_ListenerAdded);
            sharing.ListenersUpdated += new Sharing.VoidEventHandler(sharing_ListenersUpdated);
        }

        void DrawListeners()
        {
            ListenersListBox.Items.Clear();
            foreach (Listener listener in sharing.Listeners)
            {
                ListenerListBoxItem listenerListBoxItem = new ListenerListBoxItem(listener);
                ListenersListBox.Items.Add(listenerListBoxItem);
            }
        }

        void sharing_ListenersUpdated()
        {
            DrawListeners();
        }

        void sharing_ListenerAdded(Listener listener)
        {
            ListenerListBoxItem listenerListBoxItem = new ListenerListBoxItem(listener);
            ListenersListBox.Items.Add(listenerListBoxItem);
        }

        void sharing_ListenerDeleted(Listener listener)
        {
            foreach (ListenerListBoxItem listenerListBoxItem in ListenersListBox.Items)
            {
                if (listener.Id == listenerListBoxItem.Listener.Id)
                {
                    ListenersListBox.Items.Remove(listenerListBoxItem);
                    break;
                }
            }
        }

        private void DeleteAddButton_Click(object sender, RoutedEventArgs e)
        {
            if (selectedListener == null)
            {
                Listener listener = new Listener(ListenerNameTouchBox.Text, ListenerContactTouchBox.Text, (bool)ParticipateByDefaultCheckBox.IsChecked, congregation);
                sharing.AddListener(listener);
                NewListener();
            }
            else
            {
                if (SSMessageBox.Show("Are You Sure?", "Delete User", SSMessageBoxType.YesNo) == SSMessageBoxResult.Yes)
                {
                    sharing.DeleteListener(selectedListener);
                    NewListener();
                }
            }
        }

        private void CreateNewListenerButton_Click(object sender, RoutedEventArgs e)
        {
            NewListener();
        }

        void EditListener(Listener listener)
        {
            selectedListener = listener;
            AddEditLabel.Content = "Edit Listener";
            ListenerNameTouchBox.Text = listener.Name;
            ListenerContactTouchBox.Text = listener.Contact;
            ParticipateByDefaultCheckBox.IsChecked = listener.ParticipateByDefault;
            DeleteAddButtonLabel.Content = "Delete";
        }

        void NewListener()
        {
            selectedListener = null;
            ListenersListBox.SelectedIndex = -1;
            AddEditLabel.Content = "Add a New Listener";
            ListenerNameTouchBox.Text = "";
            ListenerContactTouchBox.Text = "";
            ParticipateByDefaultCheckBox.IsChecked = true;
            DeleteAddButtonLabel.Content = "Add";
        }

        private void ParticipateByDefaultCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (selectedListener != null)
                selectedListener.ParticipateByDefault = true;
        }

        private void ParticipateByDefaultCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (selectedListener != null)
                selectedListener.ParticipateByDefault = false;
        }

        private void ListenersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListenersListBox.SelectedIndex > -1)
            {
                EditListener(((ListenerListBoxItem)ListenersListBox.SelectedItem).Listener);
            }
        }

        private void ListenerContactTouchBox_ValueChanged(string _text)
        {
            if (selectedListener != null)
                selectedListener.Contact = ListenerContactTouchBox.Text;
        }

        private void ListenerNameTouchBox_ValueChanged(string _text)
        {
            if (selectedListener != null)
                selectedListener.Name = ListenerNameTouchBox.Text;
        }
    }

    class ListenerListBoxItem : ListBoxItem
    {
        private Listener listener;
        public Listener Listener
        {
            get
            {
                return listener;
            }
            set
            {
                if (listener != null)
                {
                    listener.Updated -= listener_Updated;
                }
                listener = value;
                listener.Updated += new Listener.VoidEventHandler(listener_Updated);
            }
        }

        void listener_Updated()
        {
            Draw();
        }

        void Draw()
        {
            this.Content = listener.Name;
        }

        public ListenerListBoxItem(Listener _listener)
            : base()
        {
            listener = _listener;
            listener.Updated += new Listener.VoidEventHandler(listener_Updated);
            FontSize = 14D;
            Padding = new Thickness(6);
            Margin = new Thickness(0, 0, 0, 6);
            Draw();
        }
    }
}
